package com.example.administrator.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2020/9/28.
 */

public class RegistActivity extends AppCompatActivity {

    private Button registBtn;
    private Spinner majorSp;
    private Spinner departmentSp;
    private EditText rePasswordEd;
    private EditText passwordEd;
    private EditText telephoneED;
    private EditText emailED;
    private EditText usernameED;
    private EditText nameED;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initEvent();
    }


    private void initView(){
        nameED = findViewById(R.id.regist_name_ed);
        usernameED = findViewById(R.id.regist_username_ed);
        emailED = findViewById(R.id.regist_email_ed);
        telephoneED = findViewById(R.id.regist_telephone_ed);
        passwordEd = findViewById(R.id.regist_password_ed);
        rePasswordEd = findViewById(R.id.regist_repassword_ed);

        departmentSp = findViewById(R.id.regist_department_sp);
        majorSp = findViewById(R.id.regist_majorarray_sp);
        registBtn = findViewById(R.id.regist_bt);
    }


        private void initEvent(){
        //获取部门下拉框数据
            departmentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public String departmentStr;

                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    String[] departmentArray = getResources().getStringArray(R.array.departmentarray);
                    departmentStr = departmentArray[i].toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
            majorSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public String majorStr;
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    String[] majorArry = getResources().getStringArray(R.array.majorarray);
                    majorStr = majorArry[i].toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }

            });
            registBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    initData();
                }
            });

        }


    private void initData(){
        String name = nameED.getText().toString();
        String username = usernameED.getText().toString();
        String email = emailED.getText().toString();
        String telephone = telephoneED.getText().toString();
        String password = passwordEd.getText().toString();
        String repassword = rePasswordEd.getText().toString();
        if(password.equals(repassword)){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("name",name);
                jsonObject.put("username",username);
                jsonObject.put("email",email);
                jsonObject.put("telephone",telephone);
                jsonObject.put("password",password);
                jsonObject.put("departmentSp",departmentSp);
                jsonObject.put("major",majorSp);
                jsonObject.put("role","学生");


            } catch (JSONException e) {
                e.printStackTrace();
            }
            RequestQueue requestQueue = Volley.newRequestQueue(RegistActivity.this);
            String url = "http://106.12.30.77:8080/freshmenapp/user/regist";
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                    Toast.makeText(RegistActivity.this,jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(RegistActivity.this, "网络chuxian 异常", Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);

        }else {
            Toast.makeText(this, "密码不一样", Toast.LENGTH_SHORT).show();
        }
    }
}
